#include "HandshakeSucceedCmd.h"

HandshakeSucceedCmd::HandshakeSucceedCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::CloudProtocol;
    this->m_functionCode = FunctionCode;
    this->m_serverRandom.clear();
    this->m_heartbeatIntervalTime = 0;
    this->m_heartbeatTimeoutCount = 0;
    this->m_heartbeatTimeoutTime = 0;
    this->m_serverTime.clear();
}

Package HandshakeSucceedCmd::toPackage()
{
    Package package;
    QByteArray body;

    quint16 strLength;

    strLength = m_serverRandom.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_serverRandom);

    body.append(DataTypeConver::intToByteArray(m_heartbeatIntervalTime,4));

    body.append(m_heartbeatTimeoutCount);

    body.append(DataTypeConver::intToByteArray(m_heartbeatTimeoutTime,4));

    strLength = m_serverTime.length();
    body.append(DataTypeConver::intToByteArray(strLength,2));
    body.append(m_serverTime);

   // body = encrypt(body,KeyType::AES);

    package.setId(m_cmdId);
    package.setFunction(FunctionCode);
    package.setBody(body);

    return package;
}

void HandshakeSucceedCmd::unPackage(Package package)
{
    m_cmdId = package.id();

    QByteArray body;
    quint16 strLength,index = 0;
    body=package.body();
   // body = decrypt(package.body(),KeyType::AES);

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_serverRandom = body.mid(index,strLength);
    index += strLength;

    strLength = 4;
    m_heartbeatIntervalTime = body.mid(index,strLength).toHex().toInt(0,16);
    index += strLength;

    strLength = 1;
    m_heartbeatTimeoutCount = body.mid(index,strLength).toHex().toInt(0,16);
    index += strLength;

    strLength = 4;
    m_heartbeatTimeoutTime = body.mid(index,strLength).toHex().toInt(0,16);
    index += strLength;

    strLength = body.mid(index,2).toHex().toInt(0,16);
    index += 2;
    m_serverTime = body.mid(index,strLength);
}

QByteArray HandshakeSucceedCmd::serverRandom() const
{
    return m_serverRandom;
}

void HandshakeSucceedCmd::setServerRandom(const QByteArray &serverRandom)
{
    m_serverRandom = serverRandom;
}
quint32 HandshakeSucceedCmd::heartbeatIntervalTime() const
{
    return m_heartbeatIntervalTime;
}

void HandshakeSucceedCmd::setHeartbeatIntervalTime(const quint32 &heartbeatIntervalTime)
{
    m_heartbeatIntervalTime = heartbeatIntervalTime;
}
quint8 HandshakeSucceedCmd::heartbeatTimeoutCount() const
{
    return m_heartbeatTimeoutCount;
}

void HandshakeSucceedCmd::setHeartbeatTimeoutCount(const quint8 &heartbeatTimeoutCount)
{
    m_heartbeatTimeoutCount = heartbeatTimeoutCount;
}
quint32 HandshakeSucceedCmd::heartbeatTimeoutTime() const
{
    return m_heartbeatTimeoutTime;
}

void HandshakeSucceedCmd::setHeartbeatTimeoutTime(const quint32 &heartbeatTimeoutTime)
{
    m_heartbeatTimeoutTime = heartbeatTimeoutTime;
}
QByteArray HandshakeSucceedCmd::serverTime() const
{
    return m_serverTime;
}

void HandshakeSucceedCmd::setServerTime(const QByteArray &serverTime)
{
    m_serverTime = serverTime;
}





